﻿using System;
using System.Collections.Generic;
using Autodesk.Windows;
using FsAddInManager.Utils.InitFile;
using UIFrameworkServices;

namespace FsAddInManager
{
    using System.Reflection;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.Attributes;

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class FsAddinApp : IExternalApplication
    {
        /*public Result OnShutdown(UIControlledApplication uiCtrlApp)
        {
            //清理临时文件
            TbFamilyUtil.CleanTempRfaDirectory();
            return Result.Succeeded;
        }*/

        public Result OnStartup(UIControlledApplication app)
        {
            RibbonPanel AlignPanel = app.CreateRibbonPanel("Debug");
            //Create icons in this panel
            Icons.CreateIcons(AlignPanel);

            //加载自动执行复制操作的资源文件
            Setting.AutoCopyByInitFile(); 

            //设置快捷键
            Setting.SetShortCut(AlignPanel);

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            //throw new NotImplementedException();

            return Result.Succeeded;
        }


        private static class Icons
        {
            public static void CreateIcons(RibbonPanel panel)
            {
                //Retrive dll path
                string dllPath = Assembly.GetExecutingAssembly().Location;


                //Add Align Left Button
                PushButtonData btnMain = new PushButtonData("AddInManagerCmd", "Main", dllPath, typeof(FsAddInManager).FullName);
                btnMain.ToolTip = "主界面";

                panel.AddItem(btnMain);


                PushButtonData btnFaceless = new PushButtonData("FacelessCmd", "上次", dllPath, typeof(FsAddInManagerFaceless).FullName);
                btnFaceless.ToolTip = "执行上一次命令";
                panel.AddItem(btnFaceless);


                //PushButtonData btnReadOnly = new PushButtonData("ReadOnlyCmd", "只读", dllPath, typeof(FsAddInManagerReadOnly).FullName);
                //btnReadOnly.ToolTip = "以只读方式执行命令";
                //panel.AddItem(btnReadOnly);

                PushButtonData btnSetting = new PushButtonData("SettingCmd", "设置", dllPath, typeof(FsAddinManagerSetting).FullName);
                btnSetting.ToolTip = "设置";
                panel.AddItem(btnSetting);

                /*
                PushButtonData btnAutoTrans = new PushButtonData("AutomaticCmd", "Auto事务", dllPath, typeof(FsAddInManagerAutomatic).FullName);
                panel.AddItem(btnAutoTrans);
                btnAutoTrans.ToolTip = "自动事务(不推荐)";
                */

                //todo:按钮图标


            }
        }


        public static class Setting
        {
            /// <summary>
            /// 加载“设置”按钮中勾选“自动”的资源文件
            /// </summary>
            public static void AutoCopyByInitFile()
            {
                //为“资源文件”的gridView赋值
                string dllName = "";
                InitFile inifile = new InitFile(FsAppBase.DefaultIniPath);
                bool isOK = inifile.GetValueOfKey("SelectDllDim", "DllName", ref dllName);

                if (isOK)
                {
                    string pathData = "";
                    InitFile profile = new InitFile(FsAppBase.DefaultDirPath + "\\" + dllName);
                    bool isOK2 = profile.GetValueOfKey("DataViewPathDim", "DataViewPath", ref pathData);

                    if(isOK2)
                    { 
                        List<string> rowContents = new List<string>(pathData.Split(new string[] {SettingForm.spiltRow}, StringSplitOptions.RemoveEmptyEntries));
                        foreach (var row in rowContents)
                        {
                            List<string> cellContents = new List<string>(row.Split(new string[] {SettingForm.spiltCell}, StringSplitOptions.None));
                            if (!string.IsNullOrEmpty(cellContents[2]) && Convert.ToBoolean(cellContents[2]))
                            {
                                SettingForm.CopyDirectory(cellContents[0], cellContents[1]);
                            }
                        }
                    }
                }
            }

            /// <summary>
            /// 加载“设置”按钮配置的快捷键
            /// </summary>
            public static void SetShortCut(RibbonPanel panel)
            {
                string selectKey = "";
                string repeatKey = "";
                string shortCutKey = "";

                InitFile profile = new InitFile(FsAppBase.DefaultIniPath);
                profile.GetValueOfKey("KeyBoardDim", "SelectKeyBoard", ref selectKey);
                profile.GetValueOfKey("KeyBoardDim", "RepeatKeyBoard", ref repeatKey);
                profile.GetValueOfKey("KeyBoardDim", "ShortCutKeyBoard", ref shortCutKey);

                IList<RibbonItem> items = panel.GetItems();
                foreach (var item in items)
                {
                    if (item.ToolTip == "主界面")
                    {
                        (item as RibbonButton).SetShortCut(selectKey);
                    }
                    if (item.ToolTip == "执行上一次命令")
                    {
                        (item as RibbonButton).SetShortCut(repeatKey);
                    }
                    if (item.ToolTip == "设置")
                    {
                        (item as RibbonButton).SetShortCut(shortCutKey);
                    }
                }
            }

        }


    }
}